package 移动骑士;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static int N = 310, n;
    static int[][] st = new int[N][N];
    static int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
    static int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- > 0) {
            n = in.nextInt();
            PII start = new PII(in.nextInt(), in.nextInt());
            PII end = new PII(in.nextInt(), in.nextInt());
            System.out.println(bfs(start, end));
        }
    }

    private static int bfs(PII start, PII end) {
        Queue<PII> q = new LinkedList<>();
        q.offer(start);
        for (int i = 0; i < n; i++) {
            Arrays.fill(st[i], -1);
        }
        st[start.x][start.y] = 0;
        while (!q.isEmpty()) {
            PII t = q.poll();
            if (t.x == end.x && t.y == end.y) {
                return st[end.x][end.y];
            }
            for (int i = 0; i < 8; i++) {
                int a = t.x + dx[i];
                int b = t.y + dy[i];
                if (a < 0 || a >= n || b < 0 || b >= n) {
                    continue;
                }
                if (st[a][b] != -1) {
                    continue;
                }
                st[a][b] = st[t.x][t.y] + 1;
                q.offer(new PII(a, b));
            }
        }
        return -1;
    }
}

class PII {
    int x, y;

    public PII(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
